<template>
  <div class="app-container">
    <!-- 讲师表单 -->
    <el-form ref="form" :model="teacher" label-width="80px">
      <el-form-item label="讲师名字">
        <el-input v-model="teacher.name"/>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro"/>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career"/>
      </el-form-item>
      <el-form-item label="排  序">
        <el-input v-model="teacher.sort"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level">
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="入驻时间">
        <el-col :span="11">
          <el-date-picker v-model="teacher.joinDate" type="date" placeholder="选择日期" style="width: 100%;"/>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存讲师信息</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import teacherApi from '@/api/teacher'

export default {
  data() {
    return {
      teacher: {
        sort: 0,
        level: 1 }

    }
  },
  created() {
    // console.log(this.$route.query.id)
    if (this.$route.params.id) { // js的id存在则为TRUE this.$route.params.id
      teacherApi.getTeacherById(this.$route.params.id).then(r => {
        this.teacher = r.data.item
      })
    }
  },
  methods: {
    // save 方法
    save() {
      if (this.teacher.id) {
        teacherApi.update(this.teacher).then(r => {
          this.$message({
            type: 'success',
            message: r.message
          })
          this.$router.push('/teacher')
        })
      } else {
        teacherApi.save(this.teacher).then(r => {
        // this.teacher = r.data.page.records
          this.$message({
            type: 'success',
            message: r.message
          })
          this.$router.push({ path: '/teacher' })
        })
      }
    // 更新前置方法  回显方法
    }
  }
}
</script>
